package com.culturalCenter.dataCenter.service.impl.activity;

import com.culturalCenter.dataCenter.customEntity.responseParam.activity.ActivityPromotionAnalysisResponse;
import com.culturalCenter.dataCenter.entity.activity.ActivityApply;
import com.culturalCenter.dataCenter.entity.activity.ActivityInfoCount;
import com.culturalCenter.dataCenter.mapper.activity.ActivityApplyMapper;
import com.culturalCenter.dataCenter.mapper.activity.ActivityInfoCountMapper;
import com.culturalCenter.dataCenter.service.activity.ActivityPromotionAnalysisService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.util.List;

/**
 * @author colors
 * @date 2021/1/27 15:05
 */
@Service
public class ActivityPromotionAnalysisServiceImpl implements ActivityPromotionAnalysisService {

    @Resource
    private ActivityInfoCountMapper activityInfoCountMapper;
    @Resource
    private ActivityApplyMapper activityApplyMapper;

    @Override
    public ActivityPromotionAnalysisResponse getPromotionAnalysis(String activityId) {
        ActivityPromotionAnalysisResponse activityPromotionAnalysisResponse = new ActivityPromotionAnalysisResponse();
        // 报名浏览量统计
        List<ActivityInfoCount> browseList = activityInfoCountMapper.getApplicantNumberByActivityId(activityId);
        if (CollectionUtils.isNotEmpty(browseList)) {
            activityPromotionAnalysisResponse.setBrowseList(browseList);
        }
        // 报名时段统计
        List<ActivityApply> activityApplyList = activityApplyMapper.selectTimeByActivityId(activityId);
        if (CollectionUtils.isNotEmpty(activityApplyList)) {
            int sum = activityApplyList.stream().mapToInt(ActivityApply::getSignUpNum).sum();
            activityApplyList.forEach(activityApply -> {
                        if (sum == 0) {
                            activityApply.setSignUpDate("0.00");
                        } else {
                            DecimalFormat df = new DecimalFormat("0.00");
                            double result = (double) activityApply.getSignUpNum() / sum * 100;
                            activityApply.setSignUpDate(df.format(result));
                        }
                    }
            );
            activityPromotionAnalysisResponse.setSignUpTimeList(activityApplyList);
        }
        return activityPromotionAnalysisResponse;
    }

}
